﻿namespace My.Hydrator.Helpers
{
    using System;

    public static class RandomExtensions
    {
        public static object Next<TType>(this Random random, TType minValue, TType maxValue)
        {
            var type = typeof(TType);

            if (type == typeof(int))
            {
                return random.Next(int.Parse(minValue.ToString()), int.Parse(maxValue.ToString()));
            }
            else if (type == typeof(double))
            {
                return random.NextDouble(double.Parse(minValue.ToString()), double.Parse(maxValue.ToString()));
            }
            else if (type == typeof(float))
            {
                return random.NextFloat(float.Parse(minValue.ToString()), float.Parse(maxValue.ToString()));
            }
            else if (type == typeof(decimal))
            {
                return random.NextDecimal(decimal.Parse(minValue.ToString()), decimal.Parse(maxValue.ToString()));
            }

            throw new NotSupportedException();
        }

        public static float NextFloat(this Random random, float minValue, float maxValue)
        {
            var range = maxValue - minValue;

            return (float)(range * random.NextDouble()) + minValue;
        }

        public static double NextDouble(this Random random, double minValue, double maxValue)
        {
            var range = maxValue - minValue;

            return (range * random.NextDouble()) + minValue;
        }

        public static decimal NextDecimal(this Random random, decimal minValue, decimal maxValue)
        {
            var range = maxValue - minValue;

            return (range * Convert.ToDecimal(random.NextDouble())) + minValue;
        }

        public static char NextLowerCaseLetter(this Random random)
        {
            return (char)random.Next(97, 122);
        }

        public static char NextUpperCaseLetter(this Random random)
        {
            return (char)random.Next(65, 90);
        }
    }
}
